﻿Imports System.Text.RegularExpressions

Public Class frmAddress

    Public Property address As Address
    Private isAdding As Boolean

    Private Sub frmPhone_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown

        If IsNothing(address) Then
            setupFormForAdd()
        Else
            setupFormForEdit()
        End If
    End Sub

    Private Sub lnkSaveAndClose_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkSaveAndClose.LinkClicked
        If Not areInputsValid() Then
            Return
        End If

        bindFormToAddress()

        If isAdding Then
            frmEditContact.contact.addressList.Add(address)
        End If

        frmEditContact.refreshAddresses()
        clearAndCloseForm()
    End Sub



    Private Sub lnkCancel_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkCancel.LinkClicked
        clearAndCloseForm()
    End Sub

    Private Sub setupFormForAdd()
        ' Set mode to "add" just in case it was previously "edit"
        isAdding = True
        Me.address = New Address()
        bindAddressToForm()

    End Sub

    Private Sub setupFormForEdit()

        isAdding = False
        bindAddressToForm()

    End Sub

    Private Sub clearAndCloseForm()
        Me.address = Nothing
        errProvider.Clear()
        Me.Hide()
    End Sub

    Private Sub bindAddressToForm()
        txtStreet1.Text = address.street1
        txtStreet2.Text = address.street2
        txtCity.Text = address.city
        txtState.Text = address.state
        txtZip.Text = address.zip
    End Sub

    Private Sub bindFormToAddress()

        address.street1 = txtStreet1.Text
        address.street2 = txtStreet2.Text
        address.city = txtCity.Text
        address.state = txtState.Text
        address.zip = txtZip.Text

    End Sub

    Private Function areInputsValid() As Boolean

        Dim isValid As Boolean = True
        errProvider.Clear()
        If (String.IsNullOrWhiteSpace(txtStreet1.Text)) Then
            errProvider.SetError(txtStreet1, "Required field.")
            isValid = False
        End If

        If (String.IsNullOrWhiteSpace(txtCity.Text)) Then
            errProvider.SetError(txtCity, "Required field.")
            isValid = False
        End If

        If (String.IsNullOrWhiteSpace(txtState.Text)) Then
            errProvider.SetError(txtState, "Required field.")
            isValid = False
        End If

        If (String.IsNullOrWhiteSpace(txtZip.Text)) Then
            errProvider.SetError(txtZip, "Required field.")
            isValid = False
        End If

        If (Not Regex.IsMatch(txtZip.Text, "^[0-9]{5}(-?[0-9]{4})?$")) Then
            errProvider.SetError(txtZip, "Invalid Zip.")
            isValid = False
        End If
       
        Return isValid

    End Function

    Private Sub lnkSaveAndAddAnother_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkSaveAndAddAnother.LinkClicked

        If Not areInputsValid() Then
            Return
        End If

        bindFormToAddress()

        If isAdding Then
            frmEditContact.contact.addressList.Add(address)
        End If
        setupFormForAdd()

    End Sub
End Class